<?php

class Settings extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model(array("User_model", "Setting_model", "Banned_model"));
		if (!$this->authorize->is_admin_logged_in()) {
			la_redirect("/admin/login", "请先登录");
		}
		$this->data['nav'] = 'settings';
	}
	
	function index() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$data = $this->input->post();
			foreach($data as $key => $value) {
				$this->Setting_model->add($key, $value);
			}
			$settings = $this->cache->file->delete("settings");
			$this->data = array_merge($data, $this->data);
			$this->load->view('admin/notice', $this->data);
		} else {
			$data = $this->Setting_model->find_all();
			$this->data = array_merge($data, $this->data);
			$this->load->view('admin/notice', $this->data);
		}
	}	
	
	function ip_forbidden() {
		$this->load->model(array('Forbidden_ip_model'));
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$ip = $this->input->post('ip');
			if (!empty($ip)) {
				$this->Forbidden_ip_model->add($ip);
			}
			$this->cache->file->delete("forbidden_ips");
			redirect("/console/settings/ip_forbidden");
		} else {
			$this->data['ips'] = $this->Forbidden_ip_model->find_all();
			$this->load->view('admin/ip_forbidden', $this->data);
		}
	}
	function ip_forbidden_delete() {
		$this->load->model(array('Forbidden_ip_model'));
		$id = intval($this->input->get("id"));
		$this->Forbidden_ip_model->delete($id);
		$this->cache->file->delete("forbidden_ips");
		echo  1;
		die(0);
	}
	function ip_permission() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$data = $this->input->post();
			foreach($data as $key => $value) {
				$this->Setting_model->add($key, $value);
			}
		} 
		$ip_permission_array = $this->Setting_model->find_by_key('backend_ip_permission');
		
		if (!empty($ip_permission_array)) {
			$this->data['ip_permission'] = $ip_permission_array['setting_value'];
		}
		$this->load->view('admin/ip_permission', $this->data);
	}
	
}